package com.michaelbulava.TFS.tasks.vc;

import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vcs.AbstractVcsHelper;
import com.intellij.openapi.vcs.FilePath;
import com.intellij.openapi.vcs.VcsException;
import com.intellij.openapi.vfs.VirtualFile;
import com.michaelbulava.TFS.log.FileLogger;
import com.michaelbulava.TFS.tasks.BackgroundableTask;
import com.michaelbulava.TFS.vcs.TFSVcs;
import com.michaelbulava.TFS.vcs.exceptions.NotConnectedToTFS;
import com.michaelbulava.TFS.vcs.internal.SharedOperations;
import com.michaelbulava.TFS.vcs.utils.TFSFileUtil;
import com.michaelbulava.TFS.vcs.utils.TFSProgressUtil;
import com.microsoft.tfs.core.clients.versioncontrol.WorkspaceLocation;
import com.microsoft.tfs.core.clients.versioncontrol.soapextensions.ChangeType;
import com.microsoft.tfs.core.clients.versioncontrol.soapextensions.PendingChange;
import com.microsoft.tfs.core.clients.versioncontrol.soapextensions.PendingSet;
import com.microsoft.tfs.core.clients.versioncontrol.soapextensions.Workspace;
import com.microsoft.tfs.core.clients.versioncontrol.specs.ItemSpec;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by MichaelA on 10/15/2015.
 */
public class CheckoutFileIfNeededTask extends BackgroundableTask {
    private static final Logger log = FileLogger.getInstance(CheckoutFileIfNeededTask.class);
    private final FilePath file;

    public CheckoutFileIfNeededTask(Project project, VirtualFile file) {
        super(project, "Checkout", true);
        this.file = TFSFileUtil.getFilePath(file);
    }

    @Override
    protected boolean runInBackground() {
        return true;
    }

    @Override
    public void run(ProgressIndicator indicator) {
        TFSProgressUtil.setIndeterminate(indicator, true);
        TFSProgressUtil.setProgressText(indicator, "Checking if file is part of a local workspace");
        try {
            if (!TFSVcs.getInstance(myProject).isIgnoredFile(file)){
                Workspace workspace = TFSVcs.getInstance(myProject).getWorkspaceForFile(file);
                if (workspace.getLocation() == WorkspaceLocation.LOCAL){
                    ItemSpec[] itemSpecs = TFSFileUtil.getItemSpec(myProject, workspace, file);
                    PendingSet pendingSet = workspace.getPendingChanges(itemSpecs, false);
                    boolean doCheckout = false;
                    if (pendingSet != null){
                        PendingChange[] pendingChanges = pendingSet.getPendingChanges();
                        if (pendingChanges == null || pendingChanges.length == 0){
                            doCheckout = true;
                        } else if (pendingChanges.length == 1){
                            if (pendingChanges[0].getChangeType().containsAny(ChangeType.NONE))
                                doCheckout = true;
                        } else {
                            throw new RuntimeException("Huh?");
                        }
                    } else{
                        doCheckout = true;
                    }
                    if (doCheckout) {
                        List<FilePath> files = new ArrayList<>();
                        files.add(file);
                        List<VcsException> errors = new ArrayList<>();
                        errors.addAll(SharedOperations.performCheckout(myProject, workspace, files));//, indicator);
                        if (!errors.isEmpty())
                            AbstractVcsHelper.getInstance(myProject).showErrors(errors, TFSVcs.VCS_NAME);
                    }
                }
            }
        } catch (NotConnectedToTFS notConnectedToTFS) {
            log.error("Not Connected?", notConnectedToTFS);
            notConnectedToTFS.printStackTrace();
        }
    }
}
